﻿
namespace bookMaster.AssetSystem.Windows.Forms
{
	/// <summary>
	/// abstract Ciudades typed datagridview
	/// </summary>
	[System.ComponentModel.DesignerCategoryAttribute("code")]
	public abstract class CiudadesDataGridViewBase : System.Windows.Forms.UserControl
	{	
		/// <summary> 
		/// Thrown when the current Entity changed.
		/// </summary>
		public event System.EventHandler<CiudadesDataGridViewEventArgs> CurrentEntityChanged;
		
		#region Fields
				
		/// <summary> 
		/// the current entity.
		/// </summary>
		private Entities.Ciudades _currentCiudades = null;
		//private Dictionary<string, string> _columnsHeaders = new Dictionary<string, string>();
		
		/// <summary> 
		/// the datagridview instance
		/// </summary>
		protected System.Windows.Forms.DataGridView uxCiudadesDataGridView;
		
		/// <summary> 
		/// the errorprovider
		/// </summary>
		protected System.Windows.Forms.ErrorProvider uxCiudadesErrorProvider;
		
		/// <summary> 
		/// the bindingsource
		/// </summary>
		protected System.Windows.Forms.BindingSource uxCiudadesBindingSource;
		
		
		/// <summary> 
		/// the DGV column associated with the IdCiudad property
		/// </summary>
		protected System.Windows.Forms.DataGridViewTextBoxColumn uxIdCiudadDataGridViewColumn;
		
		/// <summary> 
		/// the DGV column associated with the NombreCiudad property
		/// </summary>
		protected System.Windows.Forms.DataGridViewTextBoxColumn uxNombreCiudadDataGridViewColumn;
		
		/// <summary> 
		/// the DGV column associated with the EstadoCiudad property
		/// </summary>
		protected System.Windows.Forms.DataGridViewTextBoxColumn uxEstadoCiudadDataGridViewColumn;
		
		#endregion
					
		#region Combo list
		
		#endregion
		
		#region Main Datasource
		
		private Entities.TList<Entities.Ciudades> _CiudadesList;
				
		/// <summary> 
		/// The list of Ciudades to be displayed in the DGV.
		/// </summary>
		[System.ComponentModel.Category("Data")]
		public Entities.TList<Entities.Ciudades> CiudadesList
		{
			get {return this._CiudadesList;}
			set
			{
				this._CiudadesList = value;
				this.uxCiudadesBindingSource.DataSource = null;
				this.uxCiudadesBindingSource.DataSource = value;
				this.uxCiudadesDataGridView.DataSource = null;
				this.uxCiudadesDataGridView.DataSource = this.uxCiudadesBindingSource;				
				//this.uxCiudadesBindingSource.ListChanged += new System.ComponentModel.ListChangedEventHandler(uxCiudadesBindingSource_ListChanged);
				this.uxCiudadesBindingSource.CurrentItemChanged += new System.EventHandler(OnCiudadesBindingSourceCurrentItemChanged);
			}
		}
		
		private void OnCiudadesBindingSourceCurrentItemChanged(object sender, System.EventArgs e)
		{
			_currentCiudades = uxCiudadesBindingSource.Current as Entities.Ciudades;
			
			if (_currentCiudades != null)
			{
				_currentCiudades.Validate();
			}
			//_Ciudades.Validate();
			OnCurrentEntityChanged();
		}

		//void uxCiudadesBindingSource_ListChanged(object sender, System.ComponentModel.ListChangedEventArgs e)
		//{
			//throw new Exception("The method or operation is not implemented.");
		//}		
		
		#endregion
		
		/// <summary>
		/// Gets the currently selected <c cref="Entities.Ciudades"/> instance.
		/// </summary>
		public Entities.Ciudades SelectedCiudades
		{
			get {return this._currentCiudades;}
		}
		
		#region other properties
		
		/// <summary>
		/// Gets or sets a value indicating whether you have provided your own data-management operations for the DataGridView control.
		/// </summary>
		[System.ComponentModel.Category(".netTiers")]
		public bool VirtualMode
		{
			get {return this.uxCiudadesDataGridView.VirtualMode;}
			set
			{
				this.uxCiudadesDataGridView.VirtualMode = value;
			}
		}
		
		/// <summary>
		/// Gets or sets a value indicating whether the option to add rows is displayed to the user.
		/// </summary>
		[System.ComponentModel.Category(".netTiers")]
		public bool AllowUserToAddRows
		{
			get {return this.uxCiudadesDataGridView.AllowUserToAddRows;}
			set {this.uxCiudadesDataGridView.AllowUserToAddRows = value;}
		}
		
		/// <summary>
		/// Gets or sets a value indicating whether the user is allowed to delete rows from the DataGridView.
		/// </summary>
		[System.ComponentModel.Category(".netTiers")]
		public bool AllowUserToDeleteRows
		{
			get {return this.uxCiudadesDataGridView.AllowUserToDeleteRows;}
			set {this.uxCiudadesDataGridView.AllowUserToDeleteRows = value;}
		}
		
		/// <summary>
		/// Gets the <c cref="DataGridView"/> instance.
		/// </summary>
		[System.ComponentModel.Category(".netTiers")]
		public System.Windows.Forms.DataGridView DataGridView
		{
			get {return this.uxCiudadesDataGridView;}
		}
		
		/// <summary>
		/// Gets a collection that contains all the columns in the control.
		/// </summary>
		[System.ComponentModel.Category(".netTiers")]		
		[System.ComponentModel.DesignerSerializationVisibilityAttribute(System.ComponentModel.DesignerSerializationVisibility.Visible)]
		//[System.ComponentModel.EditorAttribute("System.Windows.Forms.Design.DataGridViewColumnCollectionEditor, System.Design", typeof(System.Drawing.Design.UITypeEditor))] 
		public System.Windows.Forms.DataGridViewColumnCollection Columns
		{
			get { return this.uxCiudadesDataGridView.Columns; }
		}
		
		#endregion
		
		/// <summary> 
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.IContainer components = null;
	
		/// <summary> 
		/// Clean up any resources being used.
		/// </summary>
		/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
		protected override void Dispose(bool disposing)
		{
			if (disposing && (components != null))
			{
				components.Dispose();
			}
			base.Dispose(disposing);
		}
	
		#region Constructor
	
		/// <summary>
		/// Initializes a new instance of the <see cref="CiudadesDataGridViewBase"/> class.
		/// </summary>
		public CiudadesDataGridViewBase()
		{			
			this.components = new System.ComponentModel.Container();			
			
			this.uxCiudadesDataGridView = new System.Windows.Forms.DataGridView();
			this.uxCiudadesBindingSource = new System.Windows.Forms.BindingSource(this.components);
			this.uxCiudadesErrorProvider = new System.Windows.Forms.ErrorProvider(this.components);
			this.uxIdCiudadDataGridViewColumn = new System.Windows.Forms.DataGridViewTextBoxColumn();
			this.uxNombreCiudadDataGridViewColumn = new System.Windows.Forms.DataGridViewTextBoxColumn();
			this.uxEstadoCiudadDataGridViewColumn = new System.Windows.Forms.DataGridViewTextBoxColumn();
			((System.ComponentModel.ISupportInitialize)(this.uxCiudadesDataGridView)).BeginInit();
			((System.ComponentModel.ISupportInitialize)(this.uxCiudadesBindingSource)).BeginInit();
			((System.ComponentModel.ISupportInitialize)(this.uxCiudadesErrorProvider)).BeginInit();
			this.SuspendLayout();
						
			// 
			// uxCiudadesErrorProvider
			// 
			this.uxCiudadesErrorProvider.ContainerControl = this;
			this.uxCiudadesErrorProvider.DataSource = this.uxCiudadesBindingSource;						
			// 
			// uxCiudadesDataGridView
			// 
			this.uxCiudadesDataGridView.AutoGenerateColumns = false;
			this.uxCiudadesDataGridView.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
			this.uxCiudadesDataGridView.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
		this.uxIdCiudadDataGridViewColumn,
		this.uxNombreCiudadDataGridViewColumn,
		this.uxEstadoCiudadDataGridViewColumn			});
			this.uxCiudadesDataGridView.Dock = System.Windows.Forms.DockStyle.Fill;
			this.uxCiudadesDataGridView.Location = new System.Drawing.Point(0, 0);
			this.uxCiudadesDataGridView.Name = "uxCiudadesDataGridView";
			this.uxCiudadesDataGridView.Size = new System.Drawing.Size(470, 300);
			this.uxCiudadesDataGridView.TabIndex = 0;	
			this.uxCiudadesDataGridView.BackgroundColor = System.Drawing.Color.WhiteSmoke;
			this.uxCiudadesDataGridView.EnableHeadersVisualStyles = false;
			this.uxCiudadesDataGridView.DataError += new System.Windows.Forms.DataGridViewDataErrorEventHandler(this.OnCiudadesDataGridViewDataError);
			this.uxCiudadesDataGridView.CellValueNeeded += new System.Windows.Forms.DataGridViewCellValueEventHandler(this.OnCiudadesDataGridViewCellValueNeeded);
			this.uxCiudadesDataGridView.CellValuePushed += new System.Windows.Forms.DataGridViewCellValueEventHandler(this.OnCiudadesDataGridViewCellValuePushed);
			
			//
			// uxIdCiudadDataGridViewColumn
			//
			this.uxIdCiudadDataGridViewColumn.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.DisplayedCells;
			this.uxIdCiudadDataGridViewColumn.DataPropertyName = "IdCiudad";
			this.uxIdCiudadDataGridViewColumn.HeaderText = "IdCiudad";
			this.uxIdCiudadDataGridViewColumn.Name = "uxIdCiudadDataGridViewColumn";
			this.uxIdCiudadDataGridViewColumn.Resizable = System.Windows.Forms.DataGridViewTriState.True;
			this.uxIdCiudadDataGridViewColumn.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.Automatic;
			this.uxIdCiudadDataGridViewColumn.ReadOnly = true;		
			//
			// uxNombreCiudadDataGridViewColumn
			//
			this.uxNombreCiudadDataGridViewColumn.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.DisplayedCells;
			this.uxNombreCiudadDataGridViewColumn.DataPropertyName = "NombreCiudad";
			this.uxNombreCiudadDataGridViewColumn.HeaderText = "NombreCiudad";
			this.uxNombreCiudadDataGridViewColumn.Name = "uxNombreCiudadDataGridViewColumn";
			this.uxNombreCiudadDataGridViewColumn.Resizable = System.Windows.Forms.DataGridViewTriState.True;
			this.uxNombreCiudadDataGridViewColumn.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.Automatic;
			this.uxNombreCiudadDataGridViewColumn.ReadOnly = false;		
			//
			// uxEstadoCiudadDataGridViewColumn
			//
			this.uxEstadoCiudadDataGridViewColumn.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.DisplayedCells;
			this.uxEstadoCiudadDataGridViewColumn.DataPropertyName = "EstadoCiudad";
			this.uxEstadoCiudadDataGridViewColumn.HeaderText = "EstadoCiudad";
			this.uxEstadoCiudadDataGridViewColumn.Name = "uxEstadoCiudadDataGridViewColumn";
			this.uxEstadoCiudadDataGridViewColumn.Resizable = System.Windows.Forms.DataGridViewTriState.True;
			this.uxEstadoCiudadDataGridViewColumn.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.Automatic;
			this.uxEstadoCiudadDataGridViewColumn.ReadOnly = false;		
			
			// this
			this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
			this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
			this.Controls.Add(this.uxCiudadesDataGridView);
			this.Name = "CiudadesDataGridView";
			this.Size = new System.Drawing.Size(470, 300);
			((System.ComponentModel.ISupportInitialize)(this.uxCiudadesErrorProvider)).EndInit();
			((System.ComponentModel.ISupportInitialize)(this.uxCiudadesDataGridView)).EndInit();
			((System.ComponentModel.ISupportInitialize)(this.uxCiudadesBindingSource)).EndInit();
			this.ResumeLayout(false);
		}
		#endregion
				
		#region events
		
		/// <summary>
		/// Raised the CurrentEntityChanged event.
		/// </summary>
		protected void OnCurrentEntityChanged()
		{
			if (CurrentEntityChanged != null)
			{
				CiudadesDataGridViewEventArgs args = new CiudadesDataGridViewEventArgs();
				args.Ciudades = _currentCiudades;
				CurrentEntityChanged(this, args); 
			}
		}
		
		#endregion
		
		/// <summary>
		/// Strongly typed event 
		/// </summary>
		public class CiudadesDataGridViewEventArgs : System.EventArgs
		{
			private Entities.Ciudades	_Ciudades;
	
			/// <summary>
			/// the  Entities.Ciudades instance.
			/// </summary>
			public Entities.Ciudades Ciudades
			{
				get { return _Ciudades; }
				set { _Ciudades = value; }
			}	
		}
		
		/// <summary>
		/// Handles the DataError event of the uxCiudadesDataGridView control.
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="e">The <see cref="T:System.Windows.Forms.DataGridViewDataErrorEventArgs"/> instance containing the event data.</param>
		protected void OnCiudadesDataGridViewDataError(object sender, System.Windows.Forms.DataGridViewDataErrorEventArgs e)
		{
			System.Diagnostics.Debug.WriteLine(string.Format( "{0}, {1}", e.ColumnIndex, e.RowIndex));
		}
		
		/// <summary>
		/// Handles the CellValueNeeded event of the uxCiudadesDataGridView control.
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="e">The <see cref="T:System.Windows.Forms.DataGridViewCellValueEventArgs"/> instance containing the event data.</param>
		protected void OnCiudadesDataGridViewCellValueNeeded(object sender, System.Windows.Forms.DataGridViewCellValueEventArgs e)
		{
			switch(uxCiudadesDataGridView.Columns[e.ColumnIndex].Name)
			{
					case "uxIdCiudadDataGridViewColumn":
						e.Value = CiudadesList[e.RowIndex].IdCiudad;
						break;
					case "uxNombreCiudadDataGridViewColumn":
						e.Value = CiudadesList[e.RowIndex].NombreCiudad;
						break;
					case "uxEstadoCiudadDataGridViewColumn":
						e.Value = CiudadesList[e.RowIndex].EstadoCiudad;
						break;
				default:
				break;
			}
		}
		
		/// <summary>
		/// Handles the CellValuePushed event of the uxCiudadesDataGridView control.
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="e">The <see cref="T:System.Windows.Forms.DataGridViewCellValueEventArgs"/> instance containing the event data.</param>
		protected void OnCiudadesDataGridViewCellValuePushed(object sender, System.Windows.Forms.DataGridViewCellValueEventArgs e)
		{
			switch(uxCiudadesDataGridView.Columns[e.ColumnIndex].Name)
			{
					case "uxIdCiudadDataGridViewColumn":
						CiudadesList[e.RowIndex].IdCiudad = (System.Int32)e.Value;
						break;
					case "uxNombreCiudadDataGridViewColumn":
						CiudadesList[e.RowIndex].NombreCiudad = (System.String)e.Value;
						break;
					case "uxEstadoCiudadDataGridViewColumn":
						CiudadesList[e.RowIndex].EstadoCiudad = (System.Int32?)e.Value;
						break;
				default:
				break;
			}
		}
	}
}
